package customize

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
)

type DiseaseApi struct{}

// CreateDisease 创建病症
// @Tags Disease
// @Summary 创建病症
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body customize.Disease true "创建病症"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /disease/createDisease [post]
func (diseaseApi *DiseaseApi) CreateDisease(c *gin.Context) {
	var disease customize.Disease
	err := c.ShouldBindJSON(&disease)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := diseaseService.CreateDisease(&disease); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteDisease 删除病症
// @Tags Disease
// @Summary 删除病症
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body customize.Disease true "删除病症"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /disease/deleteDisease [delete]
func (diseaseApi *DiseaseApi) DeleteDisease(c *gin.Context) {
	ID := c.Query("ID")
	err := diseaseService.DeleteDisease(ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteDiseaseByIds 批量删除病症
// @Tags Disease
// @Summary 批量删除病症
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /disease/deleteDiseaseByIds [delete]
func (diseaseApi *DiseaseApi) DeleteDiseaseByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	err := diseaseService.DeleteDiseaseByIds(IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateDisease 更新病症
// @Tags Disease
// @Summary 更新病症
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body customize.Disease true "更新病症"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /disease/updateDisease [put]
func (diseaseApi *DiseaseApi) UpdateDisease(c *gin.Context) {
	var cat customize.Disease
	err := c.ShouldBindJSON(&cat)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = diseaseService.UpdateDisease(cat)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindDisease 用id查询病症
// @Tags Disease
// @Summary 用id查询病症
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询病症"
// @Success 200 {object} response.Response{data=customize.Disease,msg=string} "查询成功"
// @Router /disease/findDisease [get]
func (diseaseApi *DiseaseApi) FindDisease(c *gin.Context) {
	ID := c.Query("ID")
	disease, err := diseaseService.GetDisease(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(disease, c)
}

// GetDiseaseTypeList 获取病症类型列表
// @Tags Disease
// @Summary 获取病症类型列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /disease/GetDiseaseTypeList [get]
func (diseaseApi *DiseaseApi) GetDiseaseTypeList(c *gin.Context) {
	if list, err := diseaseService.GetDiseaseTypeList(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, list, "获取成功", c)
	}
}

// GetDiseaseListByParentID 根据病症类型id查询病症
// @Tags Disease
// @Summary 根据病症类型id查询病症
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "根据病症类型id查询病症"
// @Success 200 {object} response.Response{data=customize.Disease,msg=string} "查询成功"
// @Router /disease/getDiseaseListByParentID [get]
func (diseaseApi *DiseaseApi) GetDiseaseListByParentID(c *gin.Context) {
	var db customizeReq.BzsRes
	err := c.ShouldBindJSON(&db)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	ids := strings.Split(db.Ids, ",")
	diseaseList, err := diseaseService.GetDiseaseListByParentID(ids)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(diseaseList, c)
}

// GetDiseaseList 分页获取病症列表,Tree模式下不接受参数
// @Tags Disease
// @Summary 分页获取病症列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /disease/getDiseaseList [get]
func (diseaseApi *DiseaseApi) GetDiseaseList(c *gin.Context) {
	list, err := diseaseService.GetDiseaseInfoList()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(list, "获取成功", c)
}

// SetDiseaseArticle 设置病症文章关系
func (diseaseApi *DiseaseApi) SetDiseaseArticle(c *gin.Context) {
	var req customizeReq.DiseaseArticleReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := diseaseService.SetDiseaseArticle(req); err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

// GetDiseaseArticle 获取病症文章关系
func (diseaseApi *DiseaseApi) GetDiseaseArticle(c *gin.Context) {
	var req struct {
		DiseaseID uint `json:"diseaseID" form:"diseaseID"`
	}
	err := c.ShouldBindQuery(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if list, err := diseaseService.GetDiseaseArticle(req.DiseaseID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(list, c)
	}
}
